import { defineConfig } from "vite";
import AutoImport from "unplugin-auto-import/vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import vueJsx from "@vitejs/plugin-vue-jsx";
import { appConfig } from "./appConfig/index";
function bypass(req: any, res: any, options: any) {
  const proxyURL = options.target + options.rewrite(req.url);
  res.setHeader("x-req-proxyURL", proxyURL);
}
export default ({ mode }: { mode: IEnvOType }) => {
  return defineConfig({
    base: appConfig[mode].VITE_BASE,
    plugins: [
      vueJsx(),
      vue(),
      AutoImport({
        resolvers: [ElementPlusResolver({ importStyle: "sass" })],
        imports: [
          "vue",
          "vue-router",
          {
            "@/store/modules/base": ["useAllStore"]
          },
          { "@/store/modules/drag": ["useDrag"] },
          { "@/request/request_one.ts": ["hfoRequest"] },
          { "@/request/request_two.ts": ["hftRequest"] },
        ]
      }),
      Components({ dirs: ["src/components"], resolvers: [ElementPlusResolver({ importStyle: "sass" })] })
    ],
    resolve: { alias: { "@": path.resolve(__dirname, "src") }, extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json"] },
    build: {
      minify: "terser",
      terserOptions: { compress: { drop_console: true, drop_debugger: true } },
      rollupOptions: { output: { entryFileNames: `assets/[name].[hash].js`, chunkFileNames: `assets/[name].[hash].js`, assetFileNames: `assets/[name].[hash].[ext]` } }
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@use "./src/assets/css/element.scss" as *;'
        }
      }
    },
    server: {
      port: appConfig[mode].VITE_LOCAL_PORT,
      cors: true,
      proxy: {
        "/api": {
          bypass,
          target: appConfig[mode].VITE_PROXY_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, "")
        },
        "/els/adm/tobPh": {
          bypass,
          target: appConfig[mode].VITE_PROXY_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/els\/adm\/tobPh/, "")
        }
      }
    }
  });
};
